<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>09_正则的方法</title>
  </head>

  <body>
    <!-- ceo chief executive officer -->
    <!-- execute 执行 -->
    <!-- global 全局的 -->
    <script>
      // 1. test()
      // 检测字符串是否符合要求

      // 2. exec() 执行（匹配子串+检索子串位置二合一）
      var reg = /\d{3}/g;
      var str = `abcd324sdfsdf333asdfsafd567`;

      function demo1() {
        // ['324', index: 4, input: 'abcd324sdfsdf333asdfsafd567', groups: undefined]
        console.log(reg.exec(str));

        // ["333", index: 13, input: "abcd324sdfsdf333asdfsafd567", groups: undefined]
        console.log(reg.exec(str));

        // ["567", index: 24, input: "abcd324sdfsdf333asdfsafd567", groups: undefined]
        console.log(reg.exec(str));

        // null = 检索已经到达末尾（此时游标会归零）
        console.log(reg.exec(str));

        // 游标归零后再次执行检索 结果和首次相同
        // ['324', index: 4, input: 'abcd324sdfsdf333asdfsafd567', groups: undefined]
        console.log(reg.exec(str));
      }

      /* 执行循环检索 */
      function demo2() {
        let retArr = [];
        while (retArr) {
          console.log("before", reg.lastIndex);
          retArr = reg.exec(str);
          console.log(retArr);
          console.log("after", reg.lastIndex);
          console.log("");
        }
        console.log("检索完毕");
      }

      // 手动调节游标起始位置
      reg.lastIndex = 7;
      console.log(reg.exec(str));
    </script>
  </body>
</html>
